﻿<?php
namespace Home\Controller;
use Think\Controller;
class ProductController extends Controller {
	
	public function __construct(){
		parent::__construct();
		
		if(empty($_SESSION['user']))
		{
			 //$this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
			echo "<script>alert('对不起,您还没有登录!请先登录!');top.location.href='http://192.168.0.137/index.php/Home/Login/login.html';</script>";
			//$this->redirect("Login/login"); 
		}
	}
	
	
	public function product_summary(){
		$name = $_SESSION['user'];
		$acc_type = M('account')->field('cost_price,supplier')->where(array('username'=>$name))->find();
		$sku 					= I('get.sku','','htmlspecialchars');
		$default_styles 		= I('get.default_styles','','htmlspecialchars');
		$independent_product 	= I('get.independent_product','','htmlspecialchars');
		$brand 					= I('get.brand','','htmlspecialchars');
		$logo 					= I('get.logo','','htmlspecialchars');
		$product_material 		= I('get.product_material','','htmlspecialchars');
		$product_type 			= I('get.product_type','','htmlspecialchars');
		$weight_before 			= I('get.weight_before','','htmlspecialchars');
		$weight_after			= I('get.weight_after','','htmlspecialchars');
		$cost_price_before 		= I('get.cost_price_before','','htmlspecialchars');
		$cost_price_after		= I('get.cost_price_after','','htmlspecialchars');
		$ali_lt_start 			= I('get.ali_lt_start','','htmlspecialchars');
		$ali_lt_end 			= I('get.ali_lt_end','','htmlspecialchars');
		$ali_gt_start			= I('get.ali_gt_start','','htmlspecialchars');
		$ali_gt_end 			= I('get.ali_gt_end','','htmlspecialchars');
		$aliexpress_start 		= I('get.aliexpress_start','','htmlspecialchars');
		$aliexpress_end 		= I('get.aliexpress_end','','htmlspecialchars');
		$ioffer_start 			= I('get.ioffer_start','','htmlspecialchars');
		$ioffer_end 			= I('get.ioffer_end','','htmlspecialchars');
		$custom_one 			= I('get.custom_one','','htmlspecialchars');
		$custom_two 			= I('get.custom_two','','htmlspecialchars');
		$custom_three			= I('get.custom_three','','htmlspecialchars');
		$custom_four 			= I('get.custom_four','','htmlspecialchars');
		$supplier 				= I('get.supplier','','htmlspecialchars');
		$buyer 					= I('get.buyer','','htmlspecialchars');
		$starttime 				= I('get.starttime','','htmlspecialchars');
		$endtime 				= I('get.endtime','','htmlspecialchars');
		
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		
		$res = D('product')->searchList($sku,$default_styles,$independent_product,$brand,$logo,$product_material,$product_type,$weight_before,$weight_after,$cost_price_before,$cost_price_after,$ali_lt_start,$ali_lt_end,$ali_gt_start,$ali_gt_end,$aliexpress_start,$aliexpress_end,$ioffer_start,$ioffer_end,$custom_one,$custom_two,$custom_three,$custom_four,$supplier,$buyer,$starttime,$endtime,$pageNo,$pageSize);

		
		$custom = M("custom");
		$custom_info = $custom->find();
		$where['status'] = '1';
		$product =D('product'); 
		
		
		$brand_info 	= $product->field("brand")->group('brand')->select();
		$material_info 	= $product->field("product_material")->group('product_material')->select();
		$type_info 		= $product->field("product_type")->group('product_type')->select();
		$custom_1 	= $product->field("custom_one")->group('custom_one')->select();
		$custom_2 	= $product->field("custom_two")->group('custom_two')->select();
		$custom_3 	= $product->field("custom_three")->group('custom_three')->select();
		$custom_4 	= $product->field("custom_four")->group('custom_four')->select();
		$supplier_info 	= $product->field("supplier")->group('supplier')->select();
		$buyer_info 	= $product->field("buyer")->group('buyer')->select();
		
		$product_list 	= $res['product_list'];
		$count 			= $res['count'];
		
		$this->assign('acc_type',$acc_type);
		$this->assign("brand_info",$brand_info);
		$this->assign("material_info",$material_info);
		$this->assign("type_info",$type_info);
		$this->assign("custom_1",$custom_1);
		$this->assign("custom_2",$custom_2);
		$this->assign("custom_3",$custom_3);
		$this->assign("custom_4",$custom_4);
		$this->assign("supplier_info",$supplier_info);
		$this->assign("buyer_info",$buyer_info);
		$this->assign("product_list",$result);
		$this->assign("count",$count);
		$this->assign("custom",$custom_info);
		$this->assign("product_list",$product_list);
		
		
		$this->assign("sku",$sku);
		$this->assign("default_styles",$default_styles);
		$this->assign("independent_product",$independent_product);
		$this->assign("brand",$brand);
		$this->assign("logo",$logo);
		$this->assign("product_material",$product_material);
		$this->assign("product_type",$product_type);
		$this->assign("weight_before",$weight_before);
		$this->assign("weight_after",$weight_after);
		$this->assign("cost_price_before",$cost_price_before);
		$this->assign("cost_price_after",$cost_price_after);
		$this->assign("ali_lt_start",$ali_lt_start);
		$this->assign('ali_lt_end',$ali_lt_end);
		$this->assign("ali_gt_start",$ali_gt_start);
		$this->assign("ali_gt_end",$ali_gt_end);
		$this->assign("aliexpress_start",$aliexpress_start);
		$this->assign("aliexpress_end",$aliexpress_end);
		$this->assign("ioffer_start",$ioffer_start);
		$this->assign("ioffer_end",$ioffer_end);
		$this->assign("custom_one",$custom_one);
		$this->assign("custom_two",$custom_two);
		$this->assign("custom_three",$custom_three);
		$this->assign("custom_four",$custom_four);
		$this->assign("supplier_search",$supplier);
		$this->assign("buyer_search",$buyer);
		$this->assign("starttime",$starttime);
		$this->assign('endtime',$endtime);
		
		$this->display();
	}
	
				
	
	public function impUser(){
	    $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('xls', 'xlsx');// 设置附件上传类
        $upload->rootPath  =     './Public/excel/'; // 设置附件上传根目录
        // 上传文件
        $info   =   $upload->uploadOne($_FILES['excel']);
        $filename = './Public/excel/'.$info['savepath'].$info['savename'];
        $exts = $info['ext'];
     
        if(!$info)
		 {// 上传错误提示错误信息
              $this->error($upload->getError());
         }
		 else
		 {
			vendor('PHPExcel180.PHPExcel');
			//创建PHPExcel对象，注意，不能少了\
			$PHPExcel=new \PHPExcel();
			//如果excel文件后缀名为.xls，导入这个类
			if($exts == 'xls'){
				import("Org.Util.PHPExcel.Reader.Excel5");
				$PHPReader=new \PHPExcel_Reader_Excel5();
			}else if($exts == 'xlsx'){
				import("Org.Util.PHPExcel.Reader.Excel2007");
				$PHPReader=new \PHPExcel_Reader_Excel2007();
			}
			$datazz=array();
			$data=array();
			$data_sum=array();
	
	
			//载入文件
			$PHPExcel=$PHPReader->load($filename);
			//获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
			$currentSheet=$PHPExcel->getSheet(0);
			//获取总列数
			$allColumn=$currentSheet->getHighestColumn();
			//获取总行数
			$allRow=$currentSheet->getHighestRow();
			//循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
			for($currentRow=1;$currentRow<=$allRow;$currentRow++){
				//从哪列开始，A表示第一列
				for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
					//数据坐标
					$address=$currentColumn.$currentRow;
					//读取到的数据，保存到数组$arr中
					$cell =$currentSheet->getCell($address)->getValue();
	
					if($cell instanceof PHPExcel_RichText){
						$cell  = $cell->__toString();
					}
					
					$datazz[$currentColumn]=$cell;
				}
				
				if($currentRow>1)
				{
					$data_sum[$currentRow]=$datazz;
				}
				
	            
			}  
			$product=M('product');
			$i=0;
			$str='';
			if(!empty($data_sum))
			{  
				foreach($data_sum as $key=>$v)
				{
					if(!empty($v))
					{   
					
					    $data['sku']					=$v['A'];
						$data['main_pic']				='A.jpg';
						$data['size_pic']				='B.jpg';
						$data['brand']					=$v['D'];
						$data['product_material']		=$v['F'];
						$data['product_type']			=$v['G'];
						$data['supplier']				=$v['J'];
						$data['status'] 				= '1';
							
						if($v['K']!=''){
							$time = $this->excelTime($v['K']).' 00:00:00';
						}else{
							$time = date('Y-m-d H:i:s',time());
						}
						$data['addtime']				=$time;
						
						//如果重量为空，则默认为0
						if(!empty($v['H'])){
							$data['weight']					=$v['H'];
						}
						
						//如果成本价为空，则默认为0
						if(!empty($v['I'])){
							$data['cost_price']				=$v['I'];
						}
						
						
						//如果excel表中传递过来的值是否为默认款式 如果为是，则在数据库中添加为1  否则为2
						$default_styles					=$v['B'];
						if($default_styles=='是'){
							$data['default_styles']		=1;
						}else if(empty($default_styles) || $default_styles =='否'){
							$data['default_styles']		=2;
						}
						//如果excel表中传递过来的产品类型 如果为是，则在数据库中添加为1  否则为2
						$independent_product			=$v['C'];
						if($independent_product=='是'){
							$data['independent_product']=1;
						}else if(empty($independent_product) || $independent_product =='否'){
							$data['independent_product']=2;
						}
						
						//如果excel表中传递过来的值是否进入LOGO为是，则在数据库中添加为1  否则为2
						$logo							=$v['E'];
						if($logo=='是'){
							$data['logo']				=1;
						}else if(empty($logo) || $logo =='否'){
							$data['logo']				=2;
							
						}
						
						//根据供应商关联条购员
						$buyer		  =M('supplier')->field('buyer')->where(array('for_short'=>$v['J']))->find();
						$data['buyer']					= $buyer['buyer'];
						//根据产品类型关联价格公式
						$control = M('price_control')->field('ali_lt,ali_gt,aliexpress,ioffer')->where(array('type'=>$data['product_type']))->find();
						//根据成本价与价格公式，计算不同的指导价
						if($control){
							if($control['ali_lt']==NULL){
								$control['ali_lt'] = '0';
							}
							$x = $data['cost_price'];
							$str1 = str_replace('x','$x',$control['ali_lt']);
							$str2 = str_replace('y1','$y1',$str1);
							$data['ali_lt']=eval("return $str2;");	
							
							if($control['ali_gt']==NULL){
								$control['ali_gt'] = '0';
							}
							$str3 = str_replace('x','$x',$control['ali_gt']);
							$str4 = str_replace('y2','$y2',$str3);
							$data['ali_gt']=eval("return $str4;");
							
							if($control['aliexpress']==NULL){
								$control['aliexpress'] = '0';
							}
							$str5 = str_replace('x','$x',$control['aliexpress']);
							$str6 = str_replace('y3','$y3',$str5);	
							$data['aliexpress']=eval("return $str6;");
							
							if($control['ioffer']==NULL){
								$control['ioffer'] = '0';
							}
							$str7 = str_replace('x','$x',$control['ioffer']);
							$str8 = str_replace('y4','$y4',$str7);
							$data['ioffer']=eval("return $str8;");
			
						}else{
							$data['ali_lt']		 = 0;
							$data['ali_gt']		 = 0;
							$data['aliexpress']	 = 0;
							$data['ioffer']		 = 0;
						}
						//查询产品目录里是否已有该产品，如果有则修改，没有则删除
						$check = M('product')->field('sku')->where(array('sku'=>$data['sku']))->find();
						if($check)
						{
							$res = M('product')->where(array('sku'=>$data['sku']))->save($data);
							if($res!==false)
							{
								$i++;
							}
							else
							{
								$str.=$v['A']." 导入失败<br>";
							}
							
						}
						else
						{
							$res = M('product')->add($data);
							if($res)
							{
								$i++;
							}
							else
							{
								$str.=$v['A']."<br>";
							}
						}
						
						
					}
				}
				if(!empty($str))
				{   
				    $str.="其余导入成功";
					$prompt = "<div style='font-size:26px;'>以下产品导入不成功,请及时复制出以下产品,检查后编辑再重新导入！</div><br/>";
					echo $prompt.$str;die;
				}
				else
				{
					$this->success('导入成功');
				}
				
				
			}
			else
			{
				$this->error('没有数据');
			}
			
         }
     
    }
	//php导入excel时间格式的转化
	function excelTime($days, $time=false){
		if(is_numeric($days)){
			//based on 1900-1-1
			$jd = GregorianToJD(1, 1, 1970);
			$gregorian = JDToGregorian($jd+intval($days)-25569);
			$myDate = explode('/',$gregorian);
			$myDateStr = str_pad($myDate[2],4,'0', STR_PAD_LEFT)
					."-".str_pad($myDate[0],2,'0', STR_PAD_LEFT)
					."-".str_pad($myDate[1],2,'0', STR_PAD_LEFT)
					.($time?" 00:00:00":'');
			return $myDateStr;
		}
		return $days;
	}

	
	//导出选择的产品
	public function daochu_excel(){
		$name = $_SESSION['user'];
		$acc_type = M('account')->field('cost_price,supplier')->where(array('username'=>$name))->find();
		$ids = I('get.tids','','htmlspecialchars');
		$id_list = explode(',',trim($ids,','));
		foreach($id_list as $key=>$val){
			if($acc_type['supplier'] =='1'){
			$data[$key] = M('product')->field('sku,default_styles,independent_product,brand,logo,product_material,product_type,weight,cost_price,supplier,addtime')->where(array('id'=>$val))->find();
			}else{
				$data[$key] = M('product')->field('sku,default_styles,independent_product,brand,logo,product_material,product_type,weight,cost_price,addtime')->where(array('id'=>$val))->find();
			}
			
		}
		foreach($data as $key=>$val){
			if($val['default_styles']=='1'){
				$data[$key]['default_styles']='是';
			}else{
				$data[$key]['default_styles']='否';
			}
			
			if($val['independent_product']=='1'){
				$data[$key]['independent_product']='是';
			}else{
				$data[$key]['independent_product']='否';
			}
			
			if($val['independent_product']==''){
				$data[$key]['independent_product']='';
			}
			if($val['logo']=='1'){
				$data[$key]['logo']='是';
			}else{
				$data[$key]['logo']='否';
			}
		}
		$filename = "excel";
		if($acc_type['supplier'] =='1'){
			$title = array('SKU','默认款式','线上产品','品牌','LOGO','产品材质','产品类型','重量','成本价','供应商','创建时间');
			}else{
				$title = array('SKU','默认款式','线上产品','品牌','LOGO','产品材质','产品类型','重量','成本价','创建时间');
			}
		
		$res = $this->exportexcel($data,$title,$filename);
	}
	
	
	//导出满足条件的产品
	public function search_daochu(){
		
		$sku 					= I('get.sku','','htmlspecialchars');
		$default_styles 		= I('get.default_styles','','htmlspecialchars');
		$independent_product 	= I('get.independent_product','','htmlspecialchars');
		$brand 					= I('get.brand','','htmlspecialchars');
		$logo 					= I('get.logo','','htmlspecialchars');
		$product_material 		= I('get.product_material','','htmlspecialchars');
		$product_type 			= I('get.product_type','','htmlspecialchars');
		$weight_before 			= I('get.weight_before','','htmlspecialchars');
		$weight_after			= I('get.weight_after','','htmlspecialchars');
		$cost_price_before 		= I('get.cost_price_before','','htmlspecialchars');
		$cost_price_after		= I('get.cost_price_after','','htmlspecialchars');
		$ali_lt_start 			= I('get.ali_lt_start','','htmlspecialchars');
		$ali_lt_end 			= I('get.ali_lt_end','','htmlspecialchars');
		$ali_gt_start			= I('get.ali_gt_start','','htmlspecialchars');
		$ali_gt_end 			= I('get.ali_gt_end','','htmlspecialchars');
		$aliexpress_start 		= I('get.aliexpress_start','','htmlspecialchars');
		$aliexpress_end 		= I('get.aliexpress_end','','htmlspecialchars');
		$ioffer_start 			= I('get.ioffer_start','','htmlspecialchars');
		$ioffer_end 			= I('get.ioffer_end','','htmlspecialchars');
		$custom_one 			= I('get.custom_one','','htmlspecialchars');
		$custom_two 			= I('get.custom_two','','htmlspecialchars');
		$custom_three			= I('get.custom_three','','htmlspecialchars');
		$custom_four 			= I('get.custom_four','','htmlspecialchars');
		$supplier 				= I('get.supplier','','htmlspecialchars');
		$buyer 					= I('get.buyer','','htmlspecialchars');
		$starttime 				= I('get.starttime','','htmlspecialchars');
		$endtime 				= I('get.endtime','','htmlspecialchars');
		
		$res = D('product')->searchListAll($sku,$default_styles,$independent_product,$brand,$logo,$product_material,$product_type,$weight_before,$weight_after,$cost_price_before,$cost_price_after,$ali_lt_start,$ali_lt_end,$ali_gt_start,$ali_gt_end,$aliexpress_start,$aliexpress_end,$ioffer_start,$ioffer_end,$custom_one,$custom_two,$custom_three,$custom_four,$supplier,$buyer,$starttime,$endtime);
		
		$id_list = $res['product_list'];
		$name = $_SESSION['user'];
		$acc_type = M('account')->field('cost_price,supplier')->where(array('username'=>$name))->find();
		
		
		
		foreach($id_list as $key=>$val){
			if($acc_type['supplier'] =='1'){
			$data[$key] = M('product')->field('sku,default_styles,independent_product,brand,logo,product_material,product_type,weight,cost_price,supplier,addtime')->where(array('id'=>$val['id']))->find();
			}else{
				$data[$key] = M('product')->field('sku,default_styles,independent_product,brand,logo,product_material,product_type,weight,cost_price,addtime')->where(array('id'=>$val['id']))->find();
			}
			
			
		}
		foreach($data as $key=>$val){
			if($val['default_styles']=='1'){
				$data[$key]['default_styles']='是';
			}else{
				$data[$key]['default_styles']='否';
			}
			
			if($val['independent_product']=='1'){
				$data[$key]['independent_product']='是';
			}else{
				$data[$key]['independent_product']='否';
			}
			
			if($val['logo']=='1'){
				$data[$key]['logo']='是';
			}else{
				$data[$key]['logo']='否';
			}
			
			if($val['independent_product']==''){
				$data[$key]['independent_product']='';
			}
		}
		error_reporting(E_ALL);
        date_default_timezone_set('PRC');
		  
		import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
		import("Org.Util.PHPExcel.Style.NumberFormat");
		import("Org.Util.PHPExcel.Style.Alignment");
		import("Org.Util.PHPExcel.Style.Color");
		
		import("Org.Util.PHPExcel.Style.Border");
		//设定缓存模式为经gzip压缩后存入cache（还有多种方式请百度）  
		$cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip; 
		$cacheSettings = array();  
		\PHPExcel_Settings::setCacheStorageMethod($cacheMethod,$cacheSettings);  

        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    
        $objActSheet = $objPHPExcel->getActiveSheet();
		
		if($acc_type['supplier'] =='1'){
			$objActSheet->setCellValue('A1', 'SKU');    
			$objActSheet ->setCellValue('B1', '默认款式');
			$objActSheet ->setCellValue('C1', '线上产品');
			$objActSheet->setCellValue('D1', '品牌');    
			$objActSheet ->setCellValue('E1', 'LOGO');
			$objActSheet ->setCellValue('F1', '产品材质');
			$objActSheet->setCellValue('G1', "产品类型");    
			$objActSheet ->setCellValue('H1', "重量");
			$objActSheet ->setCellValue('I1', "成本价");
			$objActSheet->setCellValue('J1', "供应商");    
			$objActSheet ->setCellValue('K1', "添加时间");
								  
			 /*以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改*/
			foreach($data as $k => $v){
				 $k +=2;
				 $objPHPExcel->setActiveSheetIndex(0);
							 //Excel的第A列，uid是你查出数组的键值，下面以此类推
				 $objActSheet->setCellValue('A'.$k, $v['sku']);    
				 $objActSheet ->setCellValue('B'.$k, $v['default_styles']);
				 $objActSheet ->setCellValue('C'.$k, $v['independent_product']);
				 
				 $objActSheet->setCellValue('D'.$k, $v['brand']);    
				 $objActSheet ->setCellValue('E'.$k, $v['logo']);
				 $objActSheet ->setCellValue('F'.$k, $v['product_material']);
				 
				 
				 $objActSheet->setCellValue('G'.$k, $v['product_type']);    
				 $objActSheet ->setCellValue('H'.$k, $v['weight']);
				 $objActSheet ->setCellValue('I'.$k, $v['cost_price']);
				 
				 $objActSheet->setCellValue('J'.$k, $v['supplier']);    
				 $objActSheet ->setCellValue('K'.$k, $v['addtime']);
            }
		}else{
			$objActSheet->setCellValue('A1', 'SKU');    
			$objActSheet ->setCellValue('B1', '默认款式');
			$objActSheet ->setCellValue('C1', '线上产品');
			$objActSheet->setCellValue('D1', '品牌');    
			$objActSheet ->setCellValue('E1', 'LOGO');
			$objActSheet ->setCellValue('F1', '产品材质');
			$objActSheet->setCellValue('G1', "产品类型");    
			$objActSheet ->setCellValue('H1', "重量");
			$objActSheet ->setCellValue('I1', "成本价");
			
			$objActSheet ->setCellValue('K1', "添加时间");
								  
			 /*以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改*/
			foreach($data as $k => $v){
				 $k +=2;
				 $objPHPExcel->setActiveSheetIndex(0);
							 //Excel的第A列，uid是你查出数组的键值，下面以此类推
				 $objActSheet->setCellValue('A'.$k, $v['sku']);    
				 $objActSheet ->setCellValue('B'.$k, $v['default_styles']);
				 $objActSheet ->setCellValue('C'.$k, $v['independent_product']);
				 
				 $objActSheet->setCellValue('D'.$k, $v['brand']);    
				 $objActSheet ->setCellValue('E'.$k, $v['logo']);
				 $objActSheet ->setCellValue('F'.$k, $v['product_material']);
				 
				 
				 $objActSheet->setCellValue('G'.$k, $v['product_type']);    
				 $objActSheet ->setCellValue('H'.$k, $v['weight']);
				 $objActSheet ->setCellValue('I'.$k, $v['cost_price']);
				 
				 $objActSheet ->setCellValue('J'.$k, $v['addtime']);
			}
		}
		$fileName = '产品目录表';
		$date = date("Y-m-d",time());
		$fileName .= "_{$date}.xls";
	
		//重命名表
		// $objPHPExcel->getActiveSheet()->setTitle('test');
		//设置活动单指数到第一个表,所以Excel打开这是第一个表
		$objPHPExcel->setActiveSheetIndex(0);
		header('Content-Type: application/vnd.ms-excel');
		header("Content-Disposition: attachment;filename=\"$fileName\"");	//图片另存为
		header('Cache-Control: max-age=0');

		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output'); //文件通过浏览器下载
		exit;
	}
	
	/**
		* 导出数据为excel表格
		*@param $data    一个二维数组,结构如同从数据库查出来的数组
		*@param $title   excel的第一行标题,一个数组,如果为空则没有标题
		*@param $filename 下载的文件名
		*@examlpe 
		$stu = M ('User');
		$arr = $stu -> select();
		exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');
	*/
	public function exportexcel($data=array(),$title=array(),$filename='report'){
		/*header("Content-Type: application/vnd.ms-execl"); 
		header("Content-Type: application/vnd.ms-excel; charset=gb2312");
		header("Content-Disposition: attachment; filename=$filename"); 
		header("Pragma: no-cache"); 
		header("Expires: 0");*/
		
		
		header("Content-type:application/octet-stream");
		header("Accept-Ranges:bytes");
		header("Content-type:applicationnd.ms-excel");  
		header("Content-Type: application/vnd.ms-excel; charset=gb2312");
		header("Content-Disposition:attachment;filename=".$filename.".xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		
		
		
		//导出xls 开始
		if (!empty($title)){
			foreach ($title as $k => $v) {
				$title[$k]=iconv("UTF-8", "GB2312",$v);
				//$title[$k]=$v;
			}
			$title= implode("\t", $title);
			echo "$title\n";
		}
		
		if (!empty($data)){
			foreach($data as $key=>$val){
				foreach ($val as $ck => $cv) {
					$data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
				}
				$data[$key]=implode("\t", $data[$key]);
				
			}
			echo implode("\n",$data);
		}
	}

	public function promoted_products(){
		$name = $_SESSION['user'];
		$acc_type = M('account')->field('cost_price,supplier')->where(array('username'=>$name))->find();
		$sku 					= I('get.sku','','htmlspecialchars');
		$default_styles 		= I('get.default_styles','','htmlspecialchars');
		$independent_product 	= I('get.independent_product','','htmlspecialchars');
		$brand 					= I('get.brand','','htmlspecialchars');
		$logo 					= I('get.logo','','htmlspecialchars');
		$product_material 		= I('get.product_material','','htmlspecialchars');
		$product_type 			= I('get.product_type','','htmlspecialchars');
		$weight_before 			= I('get.weight_before','','htmlspecialchars');
		$weight_after			= I('get.weight_after','','htmlspecialchars');
		$cost_price_before 		= I('get.cost_price_before','','htmlspecialchars');
		$cost_price_after		= I('get.cost_price_after','','htmlspecialchars');
		$ali_lt_start 			= I('get.ali_lt_start','','htmlspecialchars');
		$ali_lt_end 			= I('get.ali_lt_end','','htmlspecialchars');
		$ali_gt_start			= I('get.ali_gt_start','','htmlspecialchars');
		$ali_gt_end 			= I('get.ali_gt_end','','htmlspecialchars');
		$aliexpress_start 		= I('get.aliexpress_start','','htmlspecialchars');
		$aliexpress_end 		= I('get.aliexpress_end','','htmlspecialchars');
		$ioffer_start 			= I('get.ioffer_start','','htmlspecialchars');
		$ioffer_end 			= I('get.ioffer_end','','htmlspecialchars');
		$custom_one 			= I('get.custom_one','','htmlspecialchars');
		$custom_two 			= I('get.custom_two','','htmlspecialchars');
		$custom_three			= I('get.custom_three','','htmlspecialchars');
		$custom_four 			= I('get.custom_four','','htmlspecialchars');
		$supplier 				= I('get.supplier','','htmlspecialchars');
		$buyer 					= I('get.buyer','','htmlspecialchars');
		$starttime 				= I('get.starttime','','htmlspecialchars');
		$endtime 				= I('get.endtime','','htmlspecialchars');
		if($acc_type['supplier']==1){
			$res = D('product')->searchListAll($sku,$default_styles,$independent_product,$brand,$logo,$product_material,$product_type,$weight_before,$weight_after,$cost_price_before,$cost_price_after,$ali_lt_start,$ali_lt_end,$ali_gt_start,$ali_gt_end,$aliexpress_start,$aliexpress_end,$ioffer_start,$ioffer_end,$custom_one,$custom_two,$custom_three,$custom_four,$supplier,$buyer,$starttime,$endtime);
		}else{
			$supplier = '';
			$res = D('product')->searchListAll($sku,$default_styles,$independent_product,$brand,$logo,$product_material,$product_type,$weight_before,$weight_after,$cost_price_before,$cost_price_after,$ali_lt_start,$ali_lt_end,$ali_gt_start,$ali_gt_end,$aliexpress_start,$aliexpress_end,$ioffer_start,$ioffer_end,$custom_one,$custom_two,$custom_three,$custom_four,$supplier,$buyer,$starttime,$endtime);
		}
		$data = $res['product_list'];
        // 导出Exl
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
		import("Org.Util.PHPExcel.Style.NumberFormat");
		import("Org.Util.PHPExcel.Style.Alignment");
		import("Org.Util.PHPExcel.Style.Color");
		
		import("Org.Util.PHPExcel.Style.Border");
		
		\PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized; 	
			
		
		
        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    
        $objActSheet = $objPHPExcel->getActiveSheet();
        
        // 水平居中（位置很重要，建议在最初始位置）
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        
        // 垂直居中
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        
		//合并列单元
		//设置宽度
		// 设置个表格宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(1);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(1);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
		
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(1);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(25);
		
		
            $objDrawing[1] = new \PHPExcel_Worksheet_Drawing();
            $objDrawing[1]->setPath('Public/img/logo.png');
            // 设置宽度高度
            $objDrawing[1]->setHeight(100);//照片高度
            $objDrawing[1]->setWidth('120'); //照片宽度
            /*设置图片要插入的单元格*/
			
            $objDrawing[1]->setCoordinates('A1');
			
            // 图片偏移距离
            $objDrawing[1]->setOffsetX(30);
            $objDrawing[1]->setOffsetY(0);
            $objDrawing[1]->setWorksheet($objPHPExcel->getActiveSheet());
			
			$objPHPExcel->getActiveSheet()->mergeCells('A1:H1');
			
			$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					'size'=>'26'

                ),

            )

        );
       		$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_BOTTOM);
			$objActSheet->setCellValue('A1', 'Touch Love Jewelry Co., Ltd.');
			
            $objActSheet->getRowDimension(1)->setRowHeight(45);
			
			$objPHPExcel->getActiveSheet()->mergeCells('A2:H2');
			$objActSheet->setCellValue('A2', 'Add: R801 Tairong business building, No.63 Xizeng Road, GuangZhou, China');
            
			
			$objPHPExcel->getActiveSheet()->mergeCells('A3:H3');
			$objActSheet->setCellValue('A3', 'Email:sale@touch-love-jewelry.com  TEL:+86-20-81537159');
			
			$objPHPExcel->getActiveSheet()->getStyle('A4')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					'size'=>'14'

                ),

            )

        );

			$objPHPExcel->getActiveSheet()->mergeCells('A4:H4');
			$objActSheet->setCellValue('A4', 'Website: www.touch-love-jewelry.com');
			
			
			$objPHPExcel->getActiveSheet()->mergeCells('A5:H5');
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
       	 	$objPHPExcel->getActiveSheet()->getStyle('A5')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_BOTTOM);
			$objActSheet->setCellValue('A5', "Welcome to view our product. If you like any product, please send me the Code No. and Quantity Enjoy your shopping");
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objActSheet->getRowDimension(5)->setRowHeight(20);
			
		
			
		$objPHPExcel->getActiveSheet()->getStyle('A6')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
		
		$objActSheet->setCellValue('A6', '1');
		$objPHPExcel->getActiveSheet()->mergeCells('B6:H6');
		$objActSheet->setCellValue('B6', 'Based on FOB China price');
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('A6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('B6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);	
		
		$objActSheet->setCellValue('A7', '2');
		$objPHPExcel->getActiveSheet()->mergeCells('B7:H7');
		$objActSheet->setCellValue('B7', 'Mini Order 3 pcs / model');
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('A7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('B7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);	
			
		$count = ((count($data)%4));	//一共需要几行
		$countAll = count($data);				//一共有几条数据
		$sum_count = (ceil(sizeof($data)/4))*3+8;
		//var_dump($data);die;
		foreach($data as $key=>$value){ 
			$i=8;
			$i+=$key;
			
			//echo $i;
			if(is_int($i/4)){
				$_SESSION['y']='B';
				$_SESSION['Photo']=$key/4*3+8;
				$_SESSION['sku']=$key/4*3+9;
				$_SESSION['price']=$key/4*3+10;
				$objActSheet->setCellValue('A'.$_SESSION['Photo'], 'Photo');   
				
				$objActSheet->getRowDimension($_SESSION['Photo'])->setRowHeight(104); 
				$objActSheet->setCellValue('A'.$_SESSION['sku'], 'Code No.');  
				
				$objActSheet->getRowDimension($_SESSION['sku'])->setRowHeight(20); 
				$objActSheet->setCellValue('A'.$_SESSION['price'], 'Unit price'); 
				
				$objActSheet->getRowDimension($_SESSION['price'])->setRowHeight(40); 
				
				
			}elseif(is_int(($i+3)/4)){
				$_SESSION['y']='D';
			}elseif(is_int(($i+2)/4)){
				$_SESSION['y']='F';
			}else{
				$_SESSION['y']='H';
			}
			if(is_int($key/4)){
				$_SESSION['i']=$i;
			}
		
			
			
			$dir = "public/images/".$value['sku']; // 文件夹的名称
			$file = array();
			if (is_dir($dir)){
			  if ($dh = opendir($dir)){
				while (($file[] = readdir($dh)) !== false){
				}
				closedir($dh);
			  }
			}
			
			
			
			if( in_array("A.jpg",$file) || in_array("A.jpeg",$file) || in_array("A.png",$file) || in_array("A.gif",$file) || in_array("A.JPG",$file) || in_array("A.JPEG",$file) || in_array("A.PNG",$file) || in_array("A.GIF",$file)){
					  	$objDrawing[$i] = new \PHPExcel_Worksheet_Drawing();
						$path = $value['sku'].'/A.jpg';
						
					
						
						$objDrawing[$i]->setPath('Public/images/'.$path);
						// 设置宽度高度
						$objDrawing[$i]->setHeight(60);//照片高度
						$objDrawing[$i]->setWidth(160); //照片宽度
						/*设置图片要插入的单元格*/
						$objDrawing[$i]->setCoordinates($_SESSION['y'].$_SESSION['Photo']);
						// 图片偏移距离
						$objDrawing[$i]->setOffsetX(5);
						$objDrawing[$i]->setOffsetY(5);
						$objDrawing[$i]->setWorksheet($objPHPExcel->getActiveSheet());
						
						$objActSheet->getRowDimension($_SESSION['Photo'])->setRowHeight(130); 
						
					}else{
						$objActSheet->setCellValue($_SESSION['y'].$_SESSION['Photo'], '图片丢失'); 
						
        				$objPHPExcel->setActiveSheetIndex(0)->getStyle($_SESSION['y'].$_SESSION['Photo'])->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
						
        				$objPHPExcel->getActiveSheet()->getStyle($_SESSION['y'].$_SESSION['Photo'])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
						
				}
			
				
			$objActSheet->setCellValue($_SESSION['y'].$_SESSION['sku'], $value['sku']);   
			
 $objPHPExcel->setActiveSheetIndex(0)->getStyle($_SESSION['y'].$_SESSION['sku'])->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);       
 $objPHPExcel->getActiveSheet()->getStyle($_SESSION['y'].$_SESSION['sku'])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
 
			$objActSheet->getRowDimension($_SESSION['sku'])->setRowHeight(20);  
			$objActSheet->setCellValue($_SESSION['y'].$_SESSION['price'], 'US$'.$value['ali_lt']);
			
			
 $objPHPExcel->setActiveSheetIndex(0)->getStyle($_SESSION['y'].$_SESSION['price'])->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);       
 $objPHPExcel->getActiveSheet()->getStyle($_SESSION['y'].$_SESSION['price'])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
			
			$objActSheet->getRowDimension($_SESSION['price'])->setRowHeight(40); 
		}
        $fileName = '新品推荐表';
        $date = date("Y-m-d",time());
        $fileName .= "_{$date}.xls";

        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");	//图片另存为
        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        // END    
		
	}
	
	
	
	
	
	public function get_promoted_products(){
		$ids = trim(I('get.tids','','htmlspecialchars'),'');
		$id_list = explode(',',trim($ids,','));
		
		foreach($id_list as $k=>$v){
			$data[$k] = M('product')->where(array('id'=>$v))->find();
		}
        //var_dump($data);die;
        // 导出Exl
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
		import("Org.Util.PHPExcel.Style.NumberFormat");
		import("Org.Util.PHPExcel.Style.Alignment");
		import("Org.Util.PHPExcel.Style.Color");
		
		import("Org.Util.PHPExcel.Style.Border");
        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    
        $objActSheet = $objPHPExcel->getActiveSheet();
        
        // 水平居中（位置很重要，建议在最初始位置）
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        
        // 垂直居中
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        
		//合并列单元
		//设置宽度
		// 设置个表格宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(1);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(1);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
		
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(1);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(25);
		
		
            $objDrawing[1] = new \PHPExcel_Worksheet_Drawing();
            $objDrawing[1]->setPath('Public/img/logo.png');
            // 设置宽度高度
            $objDrawing[1]->setHeight(100);//照片高度
            $objDrawing[1]->setWidth('120'); //照片宽度
            /*设置图片要插入的单元格*/
            $objDrawing[1]->setCoordinates('A1');
			
            // 图片偏移距离
            $objDrawing[1]->setOffsetX(30);
            $objDrawing[1]->setOffsetY(0);
            $objDrawing[1]->setWorksheet($objPHPExcel->getActiveSheet());
			
			
			$objPHPExcel->getActiveSheet()->mergeCells('A1:H1');
			
       		$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					'size'=>'26'

                ),

            )

        );
			$objActSheet->setCellValue('A1', 'Touch Love Jewelry Co., Ltd.');
			
            $objActSheet->getRowDimension(1)->setRowHeight(45);
			
			$objPHPExcel->getActiveSheet()->mergeCells('A2:H2');
			$objActSheet->setCellValue('A2', 'Add: R801 Tairong business building, No.63 Xizeng Road, GuangZhou, China');
            
			
			$objPHPExcel->getActiveSheet()->mergeCells('A3:H3');
			$objActSheet->setCellValue('A3', 'Email:sale@touch-love-jewelry.com  TEL:+86-20-81537159');
			$objPHPExcel->getActiveSheet()->getStyle('A4')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					'size'=>'14'

                ),

            )

        );
			$objPHPExcel->getActiveSheet()->mergeCells('A4:H4');
			$objActSheet->setCellValue('A4', 'Website: www.touch-love-jewelry.com');
			
			$objPHPExcel->getActiveSheet()->mergeCells('A5:H5');
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
       	 	$objPHPExcel->getActiveSheet()->getStyle('A5')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_BOTTOM);
			$objActSheet->setCellValue('A5', "Welcome to view our product. If you like any product, please send me the Code No. and Quantity  Enjoy your shopping");
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objActSheet->getRowDimension(5)->setRowHeight(20);
			
			
			
		$objPHPExcel->getActiveSheet()->getStyle('A6')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
		
		$objActSheet->setCellValue('A6', '1');
		$objPHPExcel->getActiveSheet()->mergeCells('B6:H6');
		$objActSheet->setCellValue('B6', 'Based on FOB China price');	
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('B6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);	
		$objActSheet->setCellValue('A7', '2');
		$objPHPExcel->getActiveSheet()->mergeCells('B7:H7');
		$objActSheet->setCellValue('B7', 'Mini Order 3 pcs / model');
		
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('B7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			
		$count = ((count($data)%4));	//一共需要几行
		$countAll = count($data);				//一共有几条数据
		$sum_count = (ceil(sizeof($data)/4))*3+8;
		//var_dump($data);die;
		foreach($data as $key=>$value){ 
			$i=8;
			$i+=$key;
			
			//echo $i;
			if(is_int($i/4)){
				$_SESSION['y']='B';
				$_SESSION['Photo']=$key/4*3+8;
				$_SESSION['sku']=$key/4*3+9;
				$_SESSION['price']=$key/4*3+10;
				$objActSheet->setCellValue('A'.$_SESSION['Photo'], 'Photo');   
				
				$objActSheet->getRowDimension($_SESSION['Photo'])->setRowHeight(120); 
				$objActSheet->setCellValue('A'.$_SESSION['sku'], 'Code No.');  
				
				$objActSheet->getRowDimension($_SESSION['sku'])->setRowHeight(20); 
				$objActSheet->setCellValue('A'.$_SESSION['price'], 'Unit price'); 
				
				$objActSheet->getRowDimension($_SESSION['price'])->setRowHeight(40); 
			}elseif(is_int(($i+3)/4)){
				$_SESSION['y']='D';	
			}elseif(is_int(($i+2)/4)){
				$_SESSION['y']='F';
			}else{
				$_SESSION['y']='H';
			}
			if(is_int($key/4)){
				$_SESSION['i']=$i;
			}
		
			$dir = "public/images/".$value['sku']; // 文件夹的名称
			$file = array();
			if (is_dir($dir)){
			  if ($dh = opendir($dir)){
				while (($file[] = readdir($dh)) !== false){
				}
				closedir($dh);
			  }
			}
			
			
			
			if( in_array("A.jpg",$file) || in_array("A.jpeg",$file) || in_array("A.png",$file) || in_array("A.gif",$file) || in_array("A.JPG",$file) || in_array("A.JPEG",$file) || in_array("A.PNG",$file) || in_array("A.GIF",$file)){
					  	$objDrawing[$i] = new \PHPExcel_Worksheet_Drawing();
						$path = $value['sku'].'/A.jpg';
						$objDrawing[$i]->setPath('Public/images/'.$path);
						// 设置宽度高度
						$objDrawing[$i]->setHeight(60);//照片高度
						$objDrawing[$i]->setWidth(160); //照片宽度
						/*设置图片要插入的单元格*/
						$objDrawing[$i]->setCoordinates($_SESSION['y'].$_SESSION['Photo']);
						// 图片偏移距离
						$objDrawing[$i]->setOffsetX(5);
						$objDrawing[$i]->setOffsetY(5);
						$objDrawing[$i]->setWorksheet($objPHPExcel->getActiveSheet());
						
						$objActSheet->getRowDimension($_SESSION['Photo'])->setRowHeight(130); 
						
					}else{
						$objActSheet->setCellValue($_SESSION['y'].$_SESSION['Photo'], '图片丢失'); 
						
        				$objPHPExcel->setActiveSheetIndex(0)->getStyle($_SESSION['y'].$_SESSION['Photo'])->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
						
        				$objPHPExcel->getActiveSheet()->getStyle($_SESSION['y'].$_SESSION['Photo'])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
						
				}
			
				
			$objActSheet->setCellValue($_SESSION['y'].$_SESSION['sku'], $value['sku']);   
			
 $objPHPExcel->setActiveSheetIndex(0)->getStyle($_SESSION['y'].$_SESSION['sku'])->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);       
 $objPHPExcel->getActiveSheet()->getStyle($_SESSION['y'].$_SESSION['sku'])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
 
			$objActSheet->getRowDimension($_SESSION['sku'])->setRowHeight(20);  
			$objActSheet->setCellValue($_SESSION['y'].$_SESSION['price'], 'US$'.$value['ali_lt']);
			
			
 $objPHPExcel->setActiveSheetIndex(0)->getStyle($_SESSION['y'].$_SESSION['price'])->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);       
 $objPHPExcel->getActiveSheet()->getStyle($_SESSION['y'].$_SESSION['price'])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
			
			$objActSheet->getRowDimension($_SESSION['price'])->setRowHeight(40); 
		}
        $fileName = '新品推荐表';
        $date = date("Y-m-d",time());
        $fileName .= "_{$date}.xls";

        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");	//图片另存为
        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        // END    
		
	}

		//判断是否存在该图片
			function file_exists($url)
		{
		$ch = curl_init();
		curl_setopt($ch, curlopt_url,$url);
		curl_setopt($ch, curlopt_nobody, 1); // 不下载
		curl_setopt($ch, curlopt_failonerror, 1);
		curl_setopt($ch, curlopt_returntransfer, 1);
		
		if(curl_exec($ch)!==false)
		return true;
		else
		return false;
		}
			
	
	//手动添加产品
	public function add_product(){
		$supplier = M("supplier");
		$supplier_list 	= $supplier->field("for_short")->group('for_short')->select();
		
		
		if($_POST){
			$data['sku'] 					= I('post.sku','','htmlspecialchars');
			$data['main_pic'] 				= 'A.jpg';
			$data['size_pic'] 				= 'B.jpg';
			//$data['map_depot']				= I('post.map_depot','','htmlspecialchars');
			$data['default_styles'] 		= I('post.default_styles','','htmlspecialchars');
			$data['independent_product'] 	= I('post.independent_product','','htmlspecialchars');
			$data['brand'] 					= I('post.brand','','htmlspecialchars');
			$data['logo'] 					= I('post.logo','','htmlspecialchars');
			$data['product_material'] 		= I('post.product_material','','htmlspecialchars');
			$data['product_type'] 			= I('post.product_type','','htmlspecialchars');
			$data['weight'] 				= I('post.weight','','htmlspecialchars');
			$data['cost_price'] 			= I('post.cost_price','','htmlspecialchars');
			
			$control = M("price_control")->where(array('type'=>$data['product_type']))->find();
			if($control){
				$x = $data['cost_price'];
				$str1 = str_replace('x','$x',$control['ali_lt']);
				$str2 = str_replace('y1','$y1',$str1);
				$data['ali_lt']=eval("return $str2;");
				if($data['ali_lt']==NULL){
					$data['ali_lt']= 0;
				}
				
				$str3 = str_replace('x','$x',$control['ali_gt']);
				$str4 = str_replace('y2','$y2',$str3);
				$data['ali_gt']=eval("return $str4;");
				if($data['ali_gt']==NULL){
					$data['ali_gt']= 0;
				}
				$str5 = str_replace('x','$x',$control['aliexpress']);
				$str6 = str_replace('y3','$y3',$str5);
				$data['aliexpress']=eval("return $str6;");
				if($data['aliexpress']==NULL){
					$data['aliexpress']= 0;
				}
				$str7 = str_replace('x','$x',$control['ioffer']);
				$str8 = str_replace('y4','$y4',$str7);
				$data['ioffer']=eval("return $str8;");
				if($data['ioffer']==NULL){
					$data['ioffer']= 0;
				}
			}
			
			$data['custom_one'] 			= I('post.custom_one','','htmlspecialchars');
			$data['custom_two'] 			= I('post.custom_two','','htmlspecialchars');
			$data['custom_three'] 			= I('post.custom_three','','htmlspecialchars');
			$data['custom_four'] 			= I('post.custom_four','','htmlspecialchars');
			$data['supplier'] 				= I('post.supplier','','htmlspecialchars');
			$data['status'] 				= '1';
			
			
			$supplier_sql = D("supplier");
			$where['for_short'] = $data['supplier'] ;
			$buyer = $supplier_sql->buyer_list($where);
			$data['buyer'] 					= $buyer['buyer'];
			$data['addtime']				= date("Y-m-d H:i:s",time());
			$where['sku'] = $data['sku'];
			$product = M("product");
			$res = $product->where($where)->find();
			//判断是否已存在该sku
			if(empty($res)){
				$brands 		=	$data['brand'];
				$product 		=	D('product'); 
				$res_brand 		= 	$product->brand_info($brands);
				
				$product_type 		=	$data['product_type'];
				
				$type_info = M('product')->field('product_type')->where(array('product_type'=>$product_type))->find();
				if(	$type_info ==NULL){
					$add_type = M('price_control')->add(array('type'=>$data['product_type']));
				}
				$result = $product->add($data);
				
				$this->success("产品添加成功",'product_summary','3');exit;
			}else{
				$this->error("该产品已存在",'add_product','3');exit;
			}
			
		}
		
		$this->assign("supplier_list",$supplier_list);
		$this->display();
	}
	
	//修改产品信息
	public function edit_product(){
		$where['id']=$_REQUEST['id'];
		$product = M("product");
		$product_info = $product->where($where)->find();
		$custom = M("custom");
		$custom_info = $custom->find();
		$brand_info = $product->field("brand")->group('brand')->select();
		$material_info 	= $product->field("product_material")->group('product_material')->select();
		$type_info 		= $product->field("product_type")->group('product_type')->select();
		if($_POST){
			$data['sku'] 					= I('post.sku','','htmlspecialchars');
			$data['map_depot']				= I('post.map_depot','','htmlspecialchars');
			$data['default_styles'] 		= I('post.default_styles','','htmlspecialchars');
			$data['independent_product'] 	= I('post.independent_product','','htmlspecialchars');
			$data['brand'] 					= I('post.brand','','htmlspecialchars');
			$data['logo'] 					= I('post.logo','','htmlspecialchars');
			$data['product_material'] 		= I('post.product_material','','htmlspecialchars');
			$data['product_type'] 			= I('post.product_type','','htmlspecialchars');
			$data['weight'] 				= I('post.weight','','htmlspecialchars');
			$data['cost_price'] 			= I('post.cost_price','','htmlspecialchars');
			/*$data['ali_lt'] 				= I('post.ali_lt','','htmlspecialchars');
			$data['ali_gt']					= I('post.ali_gt','','htmlspecialchars');
			$data['aliexpress'] 			= I('post.aliexpress','','htmlspecialchars');
			$data['iffoer'] 				= I('post.iffoer','','htmlspecialchars');
			*/
			$data['custom_one'] 			= I('post.custom_one','','htmlspecialchars');
			$data['custom_two'] 			= I('post.custom_two','','htmlspecialchars');
			$data['custom_three'] 			= I('post.custom_three','','htmlspecialchars');
			$data['custom_four'] 			= I('post.custom_four','','htmlspecialchars');
			$data['supplier'] 				= I('post.supplier','','htmlspecialchars');
			$supplier = D("supplier");
			$for_short = $data['supplier'] ;
			$buyer = $supplier->buyer($for_short);
			$data['buyer'] 					= $buyer['buyer'];
			
			
			
			
			//根据产品类型关联价格公式
			$control = M('price_control')->field('ali_lt,ali_gt,aliexpress,ioffer')->where(array('type'=>$data['product_type']))->find();
			//根据成本价与价格公式，计算不同的指导价
			if($control['ali_lt']==NULL){
				$control['ali_lt'] = '0';
			}
			$x = $data['cost_price'];
			$str1 = str_replace('x','$x',$control['ali_lt']);
			$str2 = str_replace('y1','$y1',$str1);
			$data['ali_lt']=eval("return $str2;");	
			
			if($control['ali_gt']==NULL){
				$control['ali_gt'] = '0';
			}
			$str3 = str_replace('x','$x',$control['ali_gt']);
			$str4 = str_replace('y2','$y2',$str3);
			$data['ali_gt']=eval("return $str4;");
			
			if($control['aliexpress']==NULL){
				$control['aliexpress'] = '0';
			}
			$str5 = str_replace('x','$x',$control['aliexpress']);
			$str6 = str_replace('y3','$y3',$str5);	
			$data['aliexpress']=eval("return $str6;");
			
			if($control['ioffer']==NULL){
				$control['ioffer'] = '0';
			}
			$str7 = str_replace('x','$x',$control['ioffer']);
			$str8 = str_replace('y4','$y4',$str7);
			$data['ioffer']=eval("return $str8;");

			
			$id 		= $where['id'];
			$supplier 	= $data['supplier'] ;
			$buyer 		= $data['buyer'] ;
			$res = M("order_info")->where(array('p_id'=>$id))->save(array('supplier'=>$supplier,'buyer'=>$buyer));
			
			$result = $product->where($where)->save($data);
			$this->success("修改成功！");exit();
			
		}
		$supplier = M("supplier");
		$supplier_info 	= $supplier->field("for_short")->select();
		$this->assign("custom_info",$custom_info);
		$this->assign("material_info",$material_info);
		$this->assign("type_info",$type_info);
		$this->assign("product_info",$product_info);
		$this->assign("supplier_info",$supplier_info);
		$this->assign("brand_info",$brand_info);
		$this->display();
	}
	
	//批量修改产品信息
	public function edit_productAll(){
		$ids=I("get.tids",'','htmlspecialchars');
		$id_list = explode(',',trim($ids,','));
		
		if($_POST){
			$do = I('post.submit','','htmlspecialchars');
			if($do=='default_styles'){
				$data['default_styles'] 		= I('post.default_styles','','htmlspecialchars');
			}
			if($do=='independent_product'){
				$data['independent_product'] 	= I('post.independent_product','','htmlspecialchars');
			}
			if($do=='brand'){
				$data['brand'] 					= I('post.brand','','htmlspecialchars');
			}
			if($do=='logo'){
				$data['logo'] 					= I('post.logo','','htmlspecialchars');
			}
			if($do=='type'){
				$data['product_type'] 			= I('post.product_type','','htmlspecialchars');
			}
			if($do=='product_material'){
				$data['product_material'] 		= I('post.product_material','','htmlspecialchars');
			}
			if($do=='weight'){
				$data['weight'] 				= I('post.weight','','htmlspecialchars');
			}
			if($do=='cost_price'){
				$data['cost_price'] 			= I('post.cost_price','','htmlspecialchars');
			}
			if($do=='one'){
				$data['custom_one'] 			= I('post.custom_one','','htmlspecialchars');
			}
			if($do=='two'){
				$data['custom_two'] 			= I('post.custom_two','','htmlspecialchars');
			}
			if($do=='three'){
				$data['custom_three'] 			= I('post.custom_three','','htmlspecialchars');
			}
			if($do=='four'){
				$data['custom_four'] 			= I('post.custom_four','','htmlspecialchars');
			}
			if($do=='supplier'){
				$data['supplier'] 				= I('post.supplier','','htmlspecialchars');
				$supplier = D("supplier");
				$for_short = $data['supplier'] ;
				$buyer = $supplier->buyer($for_short);
				$data['buyer'] 					= $buyer['buyer'];
			}
			
			foreach($id_list as $val){
				
				if($do=='supplier'){
					$supplier 	= $data['supplier'] ;
					$buyer 		= $data['buyer'] ;
					$res = M("order_info")->where(array('p_id'=>$val))->save(array('supplier'=>$supplier,'buyer'=>$buyer));
				}
				
				if($do=='cost_price'){
					//根据产品类型关联价格公式
					$res = M("product")->field('product_type')->where(array('id'=>$val))->select();
					foreach($res as $k=>$v){
						$control = M('price_control')->field('ali_lt,ali_gt,aliexpress,ioffer')->where(array('type'=>$v['product_type']))->group('type')->find();
					
						//根据成本价与价格公式，计算不同的指导价
						if($control['ali_lt']==NULL  || $control['ali_lt']=='0'){
							$control['ali_lt'] = '0';
						}else{
							$x = $data['cost_price'];
							$str1 = str_replace('x','$x',$control['ali_lt']);
							$str2 = str_replace('y1','$y1',$str1);
							$data['ali_lt']=eval("return $str2;");	
						}
						
						
						if($control['ali_gt']==NULL){
							$control['ali_gt'] = '0';
						}else{
							$str3 = str_replace('x','$x',$control['ali_gt']);
							$str4 = str_replace('y2','$y2',$str3);
							$data['ali_gt']=eval("return $str4;");
						}
						
						
						if($control['aliexpress']==NULL){
							$control['aliexpress'] = '0';
						}else{
							$str5 = str_replace('x','$x',$control['aliexpress']);
							$str6 = str_replace('y3','$y3',$str5);	
							$data['aliexpress']=eval("return $str6;");
						}
						
						
						if($control['ioffer']==NULL){
							$control['ioffer'] = '0';
						}else{
							$str7 = str_replace('x','$x',$control['ioffer']);
							$str8 = str_replace('y4','$y4',$str7);
							$data['ioffer']=eval("return $str8;");
						}
						$result = M('product')->where(array('id'=>$val))->save($data);
					}
					
					
				}
				
				if($do!='cost_price'){
					$result = M('product')->where(array('id'=>$val))->save($data);
				}
			}
			$this->success("批量修改成功!");exit();
			
		}
		
		
		$supplier_info	= M('supplier')->field('full_name,for_short,buyer')->select();
		$custom_info 	= M('custom')->field('one,two,three,four')->find();
		$brand_info 	= M('product')->field("brand")->group('brand')->select();
		$material_info 	= M('product')->field("product_material")->group('product_material')->select();
		$type_info 		= M('product')->field("product_type")->group('product_type')->select();
		
		$this->assign('brand_info',$brand_info);
		$this->assign('material_info',$material_info);
		$this->assign('type_info',$type_info);
		$this->assign('supplier_info',$supplier_info);
		$this->assign('custom_info',$custom_info);
		$this->display();
	}
	
	//回收产品
	public function recycle_pro(){
		$where['id']=$_REQUEST['id'];
		$product = M("product");
		$data['status'] = "2";
		$result = $product->where($where)->save($data);
		
		if($result){
			header('Location:'.$_SERVER['HTTP_REFERER']);   
		}
	}
	
	//回收站列表
	public function recycle(){
		$name = $_SESSION['user'];
		$acc_type = M('account')->field('cost_price,supplier')->where(array('username'=>$name))->find();
		$sku = I('get.sku','','htmlspecialchars');
		$default_styles = I('get.default_styles','','htmlspecialchars');
		$independent_product = I('get.independent_product','','htmlspecialchars');
		$brand = I('get.brand','','htmlspecialchars');
		$logo = I('get.logo','','htmlspecialchars');
		
		$product_material = I('get.product_material','','htmlspecialchars');
		$product_type = I('get.product_type','','htmlspecialchars');
		$weight_before = I('get.weight_before','','htmlspecialchars');
		$weight_after = I('get.weight_after','','htmlspecialchars');
		$cost_price_before = I('get.cost_price_before','','htmlspecialchars');
		$cost_price_after = I('get.cost_price_after','','htmlspecialchars');
		$ali_lt_start = I('get.ali_lt_start','','htmlspecialchars');
		$ali_lt_end = I('get.ali_lt_end','','htmlspecialchars');
		$ali_gt_start = I('get.ali_gt_start','','htmlspecialchars');
		$ali_gt_end = I('get.ali_gt_end','','htmlspecialchars');
		$aliexpress_start = I('get.aliexpress_start','','htmlspecialchars');
		$aliexpress_end = I('get.aliexpress_end','','htmlspecialchars');
		$ioffer_start = I('get.ioffer_start','','htmlspecialchars');
		$ioffer_end = I('get.ioffer_end','','htmlspecialchars');
		$custom_one = I('get.custom_one','','htmlspecialchars');
		$custom_two = I('get.custom_two','','htmlspecialchars');
		$custom_three = I('get.custom_three','','htmlspecialchars');
		$custom_four = I('get.custom_four','','htmlspecialchars');
		$supplier = I('get.supplier','','htmlspecialchars');
		$buyer = I('get.buyer','','htmlspecialchars');
		$starttime = I('get.starttime','','htmlspecialchars');
		$endtime = I('get.endtime','','htmlspecialchars');
		
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		
		$res = D('product')->recycleList($sku,$default_styles,$independent_product,$brand,$logo,$product_material,$product_type,$weight_before,$weight_after,$cost_price_before,$cost_price_after,$ali_lt_start,$ali_lt_end,$ali_gt_start,$ali_gt_end,$aliexpress_start,$aliexpress_end,$ioffer_start,$ioffer_end,$custom_one,$custom_two,$custom_three,$custom_four,$supplier,$buyer,$starttime,$endtime,$pageNo,$pageSize);
		$recycle_list = $res['recycle_list'];

		$custom = M("custom");
		$custom_info = $custom->find();
		$where['status'] = '1';
		$product =D('product'); 
		
		
		$brand_info 	= $product->field("brand")->group('brand')->select();
		$material_info 	= $product->field("product_material")->group('product_material')->select();
		$type_info 		= $product->field("product_type")->group('product_type')->select();
		$custom_1 	= $product->field("custom_one")->group('custom_one')->select();
		$custom_2 	= $product->field("custom_two")->group('custom_two')->select();
		$custom_3 	= $product->field("custom_three")->group('custom_three')->select();
		$custom_4 	= $product->field("custom_four")->group('custom_four')->select();
		$supplier_info 	= $product->field("supplier")->group('supplier')->select();
		$buyer_info 	= $product->field("buyer")->group('buyer')->select();
		
		$product_list = $res['product_list'];
		$count = $res['count'];
	
		$this->assign("brand_info",$brand_info);
		$this->assign("material_info",$material_info);
		$this->assign("type_info",$type_info);
		$this->assign("custom_1",$custom_1);
		$this->assign("custom_2",$custom_2);
		$this->assign("custom_3",$custom_3);
		$this->assign("custom_4",$custom_4);
		$this->assign("supplier_info",$supplier_info);
		$this->assign("buyer_info",$buyer_info);
		$this->assign("product_list",$result);
		$this->assign("count",$count);
		$this->assign("custom",$custom_info);
		$this->assign('acc_type',$acc_type);
		$this->assign("recycle_list",$recycle_list);
		
		
		
		
		
		
		$this->assign("sku",$sku);
		$this->assign("default_styles",$default_styles);
		$this->assign("independent_product",$independent_product);
		$this->assign("brand",$brand);
		$this->assign("logo",$logo);
		$this->assign("product_material",$product_material);
		$this->assign("product_type",$product_type);
		$this->assign("weight_before",$weight_before);
		$this->assign("weight_after",$weight_after);
		$this->assign("cost_price_before",$cost_price_before);
		$this->assign("cost_price_after",$cost_price_after);
		$this->assign("ali_lt_start",$ali_lt_start);
		$this->assign('ali_lt_end',$ali_lt_end);
		$this->assign("ali_gt_start",$ali_gt_start);
		$this->assign("ali_gt_end",$ali_gt_end);
		$this->assign("aliexpress_start",$aliexpress_start);
		$this->assign("aliexpress_end",$aliexpress_end);
		$this->assign("ioffer_start",$ioffer_start);
		$this->assign("ioffer_end",$ioffer_end);
		$this->assign("custom_one",$custom_one);
		$this->assign("custom_two",$custom_two);
		$this->assign("custom_three",$custom_three);
		$this->assign("custom_four",$custom_four);
		$this->assign("supplier",$supplier);
		$this->assign("buyer",$buyer);
		$this->assign("starttime",$starttime);
		$this->assign('endtime',$endtime);
		$this->display();
	}
	
	
	
	
	
	
	//复原产品
	public function recover(){
		$where['id']=$_REQUEST['id'];
		$product = M("product");
		$data['status'] = "1";
		$result = $product->where($where)->save($data);
		
		if($result){
			header('Location:'.$_SERVER['HTTP_REFERER']);   
		}
	}
	
	//批量复原产品
	public function recoverAll(){
		$ids = I('get.tids','','htmlspecialchars');
		$id_list = explode(',',trim($ids,','));
		foreach($id_list as $v){
			$where['id']=$v;
			$product = M("product");
			$data['status'] = "1";
			$result = $product->where($where)->save($data);
		}
		if($result){
			header('Location:'.$_SERVER['HTTP_REFERER']);   
		}
	}
	
	//删除产品
	public function dele(){
		$where['id']=$_REQUEST['id'];
		$product = M("product");
		$result = $product->where($where)->delete();
		
		if($result){
			header('Location:'.$_SERVER['HTTP_REFERER']);   
		}
	}
	
	//批量删除产品
	public function deleAll(){
		$ids = I('get.tids','','htmlspecialchars');
		$id_list = explode(',',trim($ids,','));
		foreach($id_list as $v){
			$where['id']=$v;
			$product = M("product");
			$data['status'] = "1";
			$result = $product->where($where)->delete();
		}
		if($result){
			header('Location:'.$_SERVER['HTTP_REFERER']);   
		}
	}
	
	
	//上传图片
	public function img_upload(){
		/****************************************************************************** 
 
		参数说明: 
		$max_file_size  : 上传文件大小限制, 单位BYTE 
		$destination_folder : 上传文件路径 
		$watermark   : 是否附加水印(1为加水印,其他为不加水印); 
		 
		使用说明: 
		1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库; 
		2. 将extension_dir =改为你的php_gd2.dll所在目录; 
		******************************************************************************/  
		  
		//上传文件类型列表  
		$uptypes=array(  
			'image/jpg',  
			'image/jpeg',  
			'image/png',  
			'image/pjpeg',  
			'image/gif',  
			'image/bmp',  
			'image/x-png'  
		);  
		  
		$max_file_size=2000000;     //上传文件大小限制, 单位BYTE  
		$destination_folder="__PUBLIC__picture/"; //上传文件路径  
		$watermark=1;      //是否附加水印(1为加水印,其他为不加水印);  
		$watertype=1;      //水印类型(1为文字,2为图片)  
		$waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);  
		//$waterstring="http://www.xplore.cn/";  //水印字符串  
		$waterimg="xplore.gif";    //水印图片  
		$imgpreview=1;      //是否生成预览图(1为生成,其他为不生成);  
		$imgpreviewsize=1/2;    //缩略图比例  
		
		
		
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST')  
		{  
			if (!is_uploaded_file($_FILES["upfile"][tmp_name]))  
			//是否存在文件  
			{  
				 echo "图片不存在!";  
				 exit;  
			}  
		  
			$file = $_FILES["upfile"];  
			if($max_file_size < $file["size"])  
			//检查文件大小  
			{  
				echo "文件太大!";  
				exit;  
			}  
		  
			if(!in_array($file["type"], $uptypes))  
			//检查文件类型  
			{  
				echo "文件类型不符!".$file["type"];  
				exit;  
			}  
		  
			if(!file_exists($destination_folder))  
			{  
				mkdir($destination_folder);  
			}  
		  
			$filename=$file["tmp_name"];  
			$image_size = getimagesize($filename);  
			$pinfo=pathinfo($file["name"]);  
			$ftype=$pinfo['extension'];  
			$destination = $destination_folder.time().".".$ftype;  
			if (file_exists($destination) && $overwrite != true)  
			{  
				echo "同名文件已经存在了";  
				exit;  
			}  
		  
			if(!move_uploaded_file ($filename, $destination))  
			{  
				echo "移动文件出错";  
				exit;  
			}  
		  
			$pinfo=pathinfo($destination);  
			$fname=$pinfo[basename];  
			echo " <font color=red>已经成功上传</font><br>文件名:  <font color=blue>".$destination_folder.$fname."</font><br>";  
			echo " 宽度:".$image_size[0];  
			echo " 长度:".$image_size[1];  
			echo "<br> 大小:".$file["size"]." bytes";  
		  
			if($watermark==1)  
			{  
				$iinfo=getimagesize($destination,$iinfo);  
				$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);  
				$white=imagecolorallocate($nimage,255,255,255);  
				$black=imagecolorallocate($nimage,0,0,0);  
				$red=imagecolorallocate($nimage,255,0,0);  
				imagefill($nimage,0,0,$white);  
				switch ($iinfo[2])  
				{  
					case 1:  
					$simage =imagecreatefromgif($destination);  
					break;  
					case 2:  
					$simage =imagecreatefromjpeg($destination);  
					break;  
					case 3:  
					$simage =imagecreatefrompng($destination);  
					break;  
					case 6:  
					$simage =imagecreatefromwbmp($destination);  
					break;  
					default:  
					die("不支持的文件类型");  
					exit;  
				}  
		  
				imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);  
				imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);  
		  
				switch($watertype)  
				{  
					case 1:   //加水印字符串  
					imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);  
					break;  
					case 2:   //加水印图片  
					$simage1 =imagecreatefromgif("xplore.gif");  
					imagecopy($nimage,$simage1,0,0,0,0,85,15);  
					imagedestroy($simage1);  
					break;  
				}  
		  
				switch ($iinfo[2])  
				{  
					case 1:  
					//imagegif($nimage, $destination);  
					imagejpeg($nimage, $destination);  
					break;  
					case 2:  
					imagejpeg($nimage, $destination);  
					break;  
					case 3:  
					imagepng($nimage, $destination);  
					break;  
					case 6:  
					imagewbmp($nimage, $destination);  
					//imagejpeg($nimage, $destination);  
					break;  
				}  
		  
				//覆盖原上传文件  
				imagedestroy($nimage);  
				imagedestroy($simage);  
			}  
		  
			if($imgpreview==1)  
			{  
			echo "<br>图片预览:<br>";  
			echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);  
			echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">";  
			}  
		}  
		$this->display();
	}
	
	//进入图库
	public function get_filetree(){
	$sku = I('get.sku','','htmlspecialchars');
	$dir = "public/images/".$sku;
	if (is_dir($dir)) {
		echo "<html><head><body>";
		if ($dh = opendir($dir)) {
			while (($file = readdir($dh)) !== false) {
				if($file != "." && $file != "..") {
					echo "<img src='/$dir/"."$file'>".'<br/>';
					echo $file.'<br/>';
				}
			}
			
		}
		echo "</body></head></html>";
	}
		closedir($dh);
	}
	
	
	public function test(){
		$data = M('product')->field('sku,default_styles,independent_product,brand,logo,product_material,product_type,weight,cost_price,supplier,addtime')->where(array('id'=>'422'))->select();
		$res = $this->push111($data);
	}
	
	
	/* 导出excel函数*/
    public function push111($data,$name='Excel'){
		$name = $_SESSION['user'];
		$acc_type = M('account')->field('cost_price,supplier')->where(array('username'=>$name))->find();
          //error_reporting(E_ALL);
          date_default_timezone_set('PRC');
		  
		  import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
		import("Org.Util.PHPExcel.Style.NumberFormat");
		import("Org.Util.PHPExcel.Style.Alignment");
		import("Org.Util.PHPExcel.Style.Color");
		
		import("Org.Util.PHPExcel.Style.Border");
        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    
        $objActSheet = $objPHPExcel->getActiveSheet();
		
		
        $objActSheet->setCellValue('A1', 'SKU');    
             $objActSheet ->setCellValue('B1', '默认款式');
             $objActSheet ->setCellValue('C1', '线上产品');
			 $objActSheet->setCellValue('D1', '品牌');    
             $objActSheet ->setCellValue('E1', 'LOGO');
             $objActSheet ->setCellValue('F1', '产品材质');
			 $objActSheet->setCellValue('G1', "产品类型");    
             $objActSheet ->setCellValue('H1', "重量");
             $objActSheet ->setCellValue('I1', "成本价");
			 
			 $objActSheet->setCellValue('J1', "供应商");    
             $objActSheet ->setCellValue('K1', "添加时间");
							  
         /*以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改*/
        foreach($data as $k => $v){
             $k +=2;
             $objPHPExcel->setActiveSheetIndex(0);
                         //Excel的第A列，uid是你查出数组的键值，下面以此类推
             $objActSheet->setCellValue('A'.$k, $v['sku']);    
             $objActSheet ->setCellValue('B'.$k, $v['default_styles']);
             $objActSheet ->setCellValue('C'.$k, $v['independent_product']);
			 
			 $objActSheet->setCellValue('D'.$k, $v['brand']);    
             $objActSheet ->setCellValue('E'.$k, $v['logo']);
             $objActSheet ->setCellValue('F'.$k, $v['product_material']);
			 
			 
			 $objActSheet->setCellValue('G'.$k, $v['product_type']);    
             $objActSheet ->setCellValue('H'.$k, $v['weight']);
             $objActSheet ->setCellValue('I'.$k, $v['cost_price']);
			 
			 $objActSheet->setCellValue('J'.$k, $v['suppliersupplier']);    
             $objActSheet ->setCellValue('K'.$k, $v['addtime']);
            }
			
			$fileName = '产品目录表';
			$date = date("Y-m-d",time());
			$fileName .= "_{$date}.xls";
	
			$fileName = iconv("utf-8", "gb2312", $fileName);
			//重命名表
			// $objPHPExcel->getActiveSheet()->setTitle('test');
			//设置活动单指数到第一个表,所以Excel打开这是第一个表
			$objPHPExcel->setActiveSheetIndex(0);
			header('Content-Type: application/vnd.ms-excel');
			header("Content-Disposition: attachment;filename=\"$fileName\"");	//图片另存为
			header('Cache-Control: max-age=0');
	
			$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
			$objWriter->save('php://output'); //文件通过浏览器下载
            exit;
      }










public function daochu_excel_new(){
		$name = $_SESSION['user'];
		$acc_type = M('account')->field('cost_price,supplier')->where(array('username'=>$name))->find();
		$ids = I('get.tids','','htmlspecialchars');
		$id_list = explode(',',trim($ids,','));
		foreach($id_list as $key=>$val){
			if($acc_type['supplier'] =='1'){
			$data[$key] = M('product')->field('sku,default_styles,independent_product,brand,logo,product_material,product_type,weight,cost_price,supplier,addtime')->where(array('id'=>$val))->find();
			}else{
				$data[$key] = M('product')->field('sku,default_styles,independent_product,brand,logo,product_material,product_type,weight,cost_price,addtime')->where(array('id'=>$val))->find();
			}
			
		}
		foreach($data as $key=>$val){
			if($val['default_styles']=='1'){
				$data[$key]['default_styles']='是';
			}else{
				$data[$key]['default_styles']='否';
			}
			
			if($val['independent_product']=='1'){
				$data[$key]['independent_product']='是';
			}else{
				$data[$key]['independent_product']='否';
			}
			
			if($val['independent_product']==''){
				$data[$key]['independent_product']='';
			}
			if($val['logo']=='1'){
				$data[$key]['logo']='是';
			}else{
				$data[$key]['logo']='否';
			}
		}
		
        error_reporting(E_ALL);
        date_default_timezone_set('PRC');
		  
		import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
		import("Org.Util.PHPExcel.Style.NumberFormat");
		import("Org.Util.PHPExcel.Style.Alignment");
		import("Org.Util.PHPExcel.Style.Color");
		
		import("Org.Util.PHPExcel.Style.Border");
        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    
        $objActSheet = $objPHPExcel->getActiveSheet();
		
		if($acc_type['supplier'] =='1'){
			$objActSheet->setCellValue('A1', 'SKU');    
			$objActSheet ->setCellValue('B1', '默认款式');
			$objActSheet ->setCellValue('C1', '线上产品');
			$objActSheet->setCellValue('D1', '品牌');    
			$objActSheet ->setCellValue('E1', 'LOGO');
			$objActSheet ->setCellValue('F1', '产品材质');
			$objActSheet->setCellValue('G1', "产品类型");    
			$objActSheet ->setCellValue('H1', "重量");
			$objActSheet ->setCellValue('I1', "成本价");
			$objActSheet->setCellValue('J1', "供应商");    
			$objActSheet ->setCellValue('K1', "添加时间");
								  
			 /*以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改*/
			foreach($data as $k => $v){
				 $k +=2;
				 $objPHPExcel->setActiveSheetIndex(0);
							 //Excel的第A列，uid是你查出数组的键值，下面以此类推
				 $objActSheet->setCellValue('A'.$k, $v['sku']);    
				 $objActSheet ->setCellValue('B'.$k, $v['default_styles']);
				 $objActSheet ->setCellValue('C'.$k, $v['independent_product']);
				 
				 $objActSheet->setCellValue('D'.$k, $v['brand']);    
				 $objActSheet ->setCellValue('E'.$k, $v['logo']);
				 $objActSheet ->setCellValue('F'.$k, $v['product_material']);
				 
				 
				 $objActSheet->setCellValue('G'.$k, $v['product_type']);    
				 $objActSheet ->setCellValue('H'.$k, $v['weight']);
				 $objActSheet ->setCellValue('I'.$k, $v['cost_price']);
				 
				 $objActSheet->setCellValue('J'.$k, $v['supplier']);    
				 $objActSheet ->setCellValue('K'.$k, $v['addtime']);
            }
		}else{
			$objActSheet->setCellValue('A1', 'SKU');    
		$objActSheet ->setCellValue('B1', '默认款式');
		$objActSheet ->setCellValue('C1', '线上产品');
		$objActSheet->setCellValue('D1', '品牌');    
		$objActSheet ->setCellValue('E1', 'LOGO');
		$objActSheet ->setCellValue('F1', '产品材质');
		$objActSheet->setCellValue('G1', "产品类型");    
		$objActSheet ->setCellValue('H1', "重量");
		$objActSheet ->setCellValue('I1', "成本价");
		
		$objActSheet ->setCellValue('J1', "添加时间");
							  
         /*以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改*/
        foreach($data as $k => $v){
             $k +=2;
             $objPHPExcel->setActiveSheetIndex(0);
                         //Excel的第A列，uid是你查出数组的键值，下面以此类推
             $objActSheet->setCellValue('A'.$k, $v['sku']);    
             $objActSheet ->setCellValue('B'.$k, $v['default_styles']);
             $objActSheet ->setCellValue('C'.$k, $v['independent_product']);
			 
			 $objActSheet->setCellValue('D'.$k, $v['brand']);    
             $objActSheet ->setCellValue('E'.$k, $v['logo']);
             $objActSheet ->setCellValue('F'.$k, $v['product_material']);
			 
			 
			 $objActSheet->setCellValue('G'.$k, $v['product_type']);    
             $objActSheet ->setCellValue('H'.$k, $v['weight']);
             $objActSheet ->setCellValue('I'.$k, $v['cost_price']);
			 
             $objActSheet ->setCellValue('J'.$k, $v['addtime']);
		}
			
			
		/*$filename = "excel";
		if($acc_type['supplier'] =='1'){
			$title = array('SKU','默认款式','线上产品','品牌','LOGO','产品材质','产品类型','重量','成本价','供应商','创建时间');
			}else{
				$title = array('SKU','默认款式','线上产品','品牌','LOGO','产品材质','产品类型','重量','成本价','创建时间');
			}
		
		$res = $this->exportexcel($data,$title,$filename);*/
	}
	$fileName = '产品目录表';
			$date = date("Y-m-d",time());
			$fileName .= "_{$date}.xls";
		
			//重命名表
			// $objPHPExcel->getActiveSheet()->setTitle('test');
			//设置活动单指数到第一个表,所以Excel打开这是第一个表
			$objPHPExcel->setActiveSheetIndex(0);
			header('Content-Type: application/vnd.ms-excel');
			header("Content-Disposition: attachment;filename=\"$fileName\"");	//图片另存为
			header('Cache-Control: max-age=0');
	
			$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
			$objWriter->save('php://output'); //文件通过浏览器下载
            exit;
	}
}

















